// ignore_for_file: non_constant_identifier_names, prefer_const_constructors, constant_identifier_names

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter/cupertino.dart';

const Color BLUE = Color(0xFF346FEB);
const Color BG_GREY = Color(0xFFF5F7FB);

final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

Color hexToColor(String hexString) {
  final buffer = StringBuffer();
  if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');
  buffer.write(hexString.replaceFirst('#', ''));
  return Color(int.parse(buffer.toString(), radix: 16));
}

void log(String message) {
  final timestamp = DateTime.now().toString();
  print('[$timestamp]:  $message');
}

void toast(String text, {int milliseconds = 2000}) {
  EasyLoading.show(status: (text));
  // 3秒后自动关闭
  Future.delayed(Duration(milliseconds: milliseconds), () {
    EasyLoading.dismiss();
  });
}

void openCupertinoDialog(String text, void Function() callback) {
  EasyLoading.dismiss();
  showCupertinoDialog(
    context: navigatorKey.currentState!.context,
    barrierDismissible: true,
    builder: (BuildContext context) {
      return CupertinoAlertDialog(
        title: const Text(
          'Notice',
          textAlign: TextAlign.center,
        ),
        content: Text(text),
        actions: [
          CupertinoDialogAction(
            child: const Text('Cancel', style: TextStyle(color: Colors.black)),
            onPressed: () {
              Navigator.pop(context, null); // 关闭对话框
            },
          ),
          CupertinoDialogAction(
            child: const Text('Confirm', style: TextStyle(color: BLUE)),
            onPressed: () {
              Navigator.pop(context); // 关闭对话框
              callback();
            },
          ),
        ],
      );
    },
  );
}
